"""
金融数据平台主入口文件
"""
import logging
import uvicorn
from fastapi import FastAPI
from modules.api.main import app
from config.settings import API_HOST, API_PORT, LOG_LEVEL, LOG_FORMAT
from modules.models.base import Base, engine

# 配置日志
logging.basicConfig(
    level=getattr(logging, LOG_LEVEL),
    format=LOG_FORMAT
)
logger = logging.getLogger("app")


def init_db():
    """初始化数据库表结构"""
    try:
        # 导入所有模型，确保被映射到元数据
        from modules.models import stock
        
        # 创建表
        Base.metadata.create_all(bind=engine)
        logger.info("数据库表结构初始化完成")
    except Exception as e:
        logger.error(f"数据库表结构初始化失败: {str(e)}")
        raise


if __name__ == "__main__":
    # 初始化数据库
    init_db()
    
    # 启动API服务
    logger.info(f"启动API服务, 地址: {API_HOST}:{API_PORT}")
    uvicorn.run(app, host=API_HOST, port=API_PORT)